package com.wyw.design.model.learning.behavioral.iteratorpattern;

/**
 * @Title   具体迭代器
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/4/18 10:06
 * Update History:
 * Author        Time            Content
 */
public class ConcreteIterator implements Iterator {
    private ConcreteAggregate aggregate;
    private int index;
    private int size;
    public ConcreteIterator(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
        this.index = 0;
        this.size = aggregate.size();
    }
    // 是否有下一个元素
    @Override
    public boolean hasNext() {
        return index < size;
    }
    // 返回下一个元素
    @Override
    public Object next() {
        if (index < size) {
            return aggregate.getElement(index++);
        }
        return null;
    }
}
